const {
    Products
} = require('../models/productsModel');

const {
    moveFiles,
    removeFiles
} = require('../ulities/upload');

//获取产品数据
async function getProducts(req, res, next) {
    const {
        type = 'name', value = '', pageSize = 4, currentPage = 1
    } = req.query;
    // 总条数
    let total = await Products.countDocuments();
    // 总页数
    let totalPages = Math.ceil(total / pageSize);
    // 模糊查询
    if (value) {
        const totalData = await Products.find({
                [type]: {
                    $regex: value,
                    $options: '$i'
                }
            }) // 关联查询
        total = totalData.length;
        totalPages = Math.ceil(totalData.length / pageSize);
    }

    let data = await Products
        .find({
            [type]: {
                $regex: value,
                $options: '$i'
            }
        })
        .populate("classifyId") //关联
        .limit(pageSize - 0) // 设置请求的条数
        .skip((currentPage - 1) * pageSize) // 设置跳过的条数
    res.send({
        status: 1,
        data: {
            total,
            totalPages,
            data,
            pageSize,
            currentPage
        },
        message: '产品数据获取成功'
    })
}

//新增产品
async function addProduct(req, res) {
    const {
        name,
        price,
        information,
        year,
        imgURL,
        classifyId
    } = req.body;
    await Products.create({
        name,
        price,
        information,
        year,
        imgURL,
        classifyId
    });
    // 移动图片 
    if (imgURL) {
        moveFiles({
            fromPath: './public/temp',
            toPath: './public/images',
            filename: imgURL
        });
        removeFiles('./public/temp');
    }
    res.send({
        status: 1,
        message: '产品新增成功'
    })
}

//删除商品
async function deleteProduct(req, res) {
    const {
        _id
    } = req.body;
    try {
        const data = await Products.findByIdAndDelete(_id);
        if (data) {
            res.send({
                status: 1,
                message: '删除成功'
            })
        } else {
            res.send({
                status: 0,
                message: '删除失败'
            })
        }
    } catch (error) {
        console.log('删除失败', error);
        res.send({
            status: 0,
            message: '删除失败'
        })
    }
}

//获得一个要新增的人
async function getOnePro(req, res) {
    const {
        _id
    } = req.body;
    try {
        const data = await Products.findById(_id).populate("classifyId");
        res.send({
            status: 1,
            data,
            message: '获取成功'
        })
    } catch (error) {
        res.send({
            status: 0,
            message: '获取失败'
        })
    }
}

//修改产品信息
async function UpdateOnePro(req, res) {
    const {
        _id,
        name,
        price,
        information,
        year,
        imgURL,
        classifyId
    } = req.body;
    const data = await Products.updateOne({
        _id
    }, {
        name,
        price,
        information,
        year,
        imgURL,
        classifyId
    });
    console.log(data);
    if (data.nModified === 1) {
        res.send({
            status: 1,
            message: '修改成功'
        })
    } else {
        res.send({
            status: 0,
            message: '修改失败'
        })
    }
}

module.exports = {
    addProduct,
    getProducts,
    deleteProduct,
    getOnePro,
    UpdateOnePro
}